/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pantallaBuscarEmpresaCliente.java
 *
 * Created on 17/08/2010, 20:17:26
 */

package vista.comer;

import controlador.comer.GestorBuscarEmpresaCliente;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.RowFilter;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import util.NTupla;
import util.StringUtil;
import util.SwingPanel;
import vista.interfaces.IAyuda;
import vista.interfaces.ICallBack;

/**
 *
 * @author Emmanuel
 */
public class pantallaBuscarEmpresaCliente extends javax.swing.JInternalFrame  implements IAyuda,ICallBack{
    private GestorBuscarEmpresaCliente gestor;
    private DefaultTableModel dtm;
    private TableRowSorter<TableModel> elQueOrdena;
    /** Creates new form pantallaBuscarEmpresaCliente */
    public pantallaBuscarEmpresaCliente() {
        gestor = new GestorBuscarEmpresaCliente(this);
        initComponents();
        tablaEmpresaCliente.setPreferredScrollableViewportSize(tablaEmpresaCliente.getPreferredSize());
        llenarTabla();
    }

    private void llenarTabla() {
        ArrayList<NTupla> empresas = gestor.getEmpresasCliente();
        dtm = new DefaultTableModel();
        dtm.addColumn("Nro.");
        dtm.addColumn("Razón Social");
        dtm.addColumn("CUIT");
        dtm.addColumn("E-Mail");
        dtm.addColumn("Estado");
        dtm.addColumn("Pagina Web");
        dtm.addColumn("Plantas");

        Iterator it = empresas.iterator();
        Iterator itdatos;
        while (it.hasNext()){
            NTupla tu = (NTupla)it.next();
            Object[] item = new Object[9];
            item[0] = tu.getId();
            item[1] = (String)tu.getNombre();
            ArrayList<String> datos = (ArrayList<String>)tu.getData();
            itdatos = datos.iterator();
            int i = 2;
            String dato= "";
            while(itdatos.hasNext()){
                dato = (String)itdatos.next();
                item[i] = dato;
                i++;
            }
            dtm.addRow(item);
        }
        tablaEmpresaCliente.setModel(dtm);
        elQueOrdena = new TableRowSorter<TableModel>(dtm);
        tablaEmpresaCliente.setRowSorter(elQueOrdena);
    }

    public int getIdEmpresaClienteSeleccionado(){
        int tipo = -1;
         if((tablaEmpresaCliente.getSelectedRowCount())==1)
        {
            DefaultTableModel modelo = (DefaultTableModel) tablaEmpresaCliente.getModel();
            tipo = (Integer) modelo.getValueAt(tablaEmpresaCliente.getSelectedRow(), 0);
        }
        return tipo;
    }

    private void FiltrarTabla(){
       TableRowSorter<TableModel> modeloOrdenado;
       modeloOrdenado = new TableRowSorter<TableModel>(tablaEmpresaCliente.getModel());
       tablaEmpresaCliente.setRowSorter(modeloOrdenado);

           String[] cadena=txtBuscar.getText().trim().split(" ");
           List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>();
           for (int i= 0; i < cadena.length; i++)
           {
             filters.add(RowFilter.regexFilter("(?i)" + cadena[i]));
           }
           RowFilter<Object,Object> cadenaFilter = RowFilter.andFilter(filters);
           modeloOrdenado.setRowFilter(cadenaFilter);

           // CAMBIO LOS TAMAÑOS DE LAS FILAS
           DefaultTableModel modelo = (DefaultTableModel) tablaEmpresaCliente.getModel();
           for (int i = 0; i < modelo.getRowCount(); i++)
           {
            // REDIMENSIONO LA FILA !!! -----------------------------------
                int index = modeloOrdenado.convertRowIndexToView(i);
                if(index>-1)
                {
                    // ESTA
                    String item = (String) modelo.getValueAt(i,2);
                    int cantItems = StringUtil.cantidadOcurrencias(item,"<b>x</b>");
                    if(cantItems!=0)
                    {
                        tablaEmpresaCliente.setRowHeight(index,16*cantItems);
                    }
                    //LogUtil.addDebug("ConsultarPreciosXProveedor: Cantidad de Repeticiones: "+cantItems);
                }
                // REDIMENSIONO LA FILA !!! -----------------------------------
           }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        btnConsultarEmpresaCliente = new javax.swing.JButton();
        rbTodas = new javax.swing.JRadioButton();
        btnModificarEmpresaCliente = new javax.swing.JButton();
        btnRegistrarEmpresaCliente = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        btnDarBajaEmpresaCliente = new javax.swing.JButton();
        txtBuscar = new javax.swing.JTextField();
        rbAltas = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaEmpresaCliente = new javax.swing.JTable();
        rbBajas = new javax.swing.JRadioButton();

        buttonGroup1.add(rbTodas);
        buttonGroup1.add(rbAltas);
        buttonGroup1.add(rbBajas);

        btnConsultarEmpresaCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/search_page.png"))); // NOI18N
        btnConsultarEmpresaCliente.setText("Ver Detalles");
        btnConsultarEmpresaCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarEmpresaClienteActionPerformed(evt);
            }
        });

        rbTodas.setText("Todas");
        rbTodas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbTodasActionPerformed(evt);
            }
        });

        btnModificarEmpresaCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/text_page.png"))); // NOI18N
        btnModificarEmpresaCliente.setText("Modificar");
        btnModificarEmpresaCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarEmpresaClienteActionPerformed(evt);
            }
        });

        btnRegistrarEmpresaCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/add_page.png"))); // NOI18N
        btnRegistrarEmpresaCliente.setText("Nuevo");
        btnRegistrarEmpresaCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarEmpresaClienteActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/delete.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/search.png"))); // NOI18N

        btnDarBajaEmpresaCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/delete_page.png"))); // NOI18N
        btnDarBajaEmpresaCliente.setText("Dar de Baja");
        btnDarBajaEmpresaCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDarBajaEmpresaClienteActionPerformed(evt);
            }
        });

        txtBuscar.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        txtBuscar.setForeground(new java.awt.Color(102, 102, 102));
        txtBuscar.setText("Buscar...");
        txtBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtBuscarMouseClicked(evt);
            }
        });
        txtBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBuscarActionPerformed(evt);
            }
        });
        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtBuscarKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBuscarKeyReleased(evt);
            }
        });

        rbAltas.setText("Altas");
        rbAltas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbAltasActionPerformed(evt);
            }
        });

        tablaEmpresaCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Legajo", "Nombre", "Apellido"
            }
        ));
        tablaEmpresaCliente.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPane1.setViewportView(tablaEmpresaCliente);

        rbBajas.setText("Bajas");
        rbBajas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbBajasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(rbTodas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbAltas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbBajas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 265, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(btnConsultarEmpresaCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRegistrarEmpresaCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnModificarEmpresaCliente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDarBajaEmpresaCliente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(rbTodas)
                        .addComponent(rbAltas)
                        .addComponent(rbBajas)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDarBajaEmpresaCliente)
                    .addComponent(btnModificarEmpresaCliente)
                    .addComponent(btnRegistrarEmpresaCliente)
                    .addComponent(btnConsultarEmpresaCliente)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConsultarEmpresaClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarEmpresaClienteActionPerformed
        pantallaConsultarEmpresaCliente p = new pantallaConsultarEmpresaCliente(this.getIdEmpresaClienteSeleccionado());
        SwingPanel.getInstance().addWindow(p);
        p.setVisible(true);
}//GEN-LAST:event_btnConsultarEmpresaClienteActionPerformed

    private void btnModificarEmpresaClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarEmpresaClienteActionPerformed
        pantallaModificarEmpresaCliente p = new pantallaModificarEmpresaCliente(this);
        SwingPanel.getInstance().addWindow(p);
        p.setVisible(true);
}//GEN-LAST:event_btnModificarEmpresaClienteActionPerformed

    private void btnRegistrarEmpresaClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarEmpresaClienteActionPerformed
        pantallaRegistrarEmpresaCliente p = new pantallaRegistrarEmpresaCliente(this);
        SwingPanel.getInstance().addWindow(p);
        p.setVisible(true);
}//GEN-LAST:event_btnRegistrarEmpresaClienteActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnDarBajaEmpresaClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDarBajaEmpresaClienteActionPerformed
        pantallaDarBajaEmpresaCliente p = new pantallaDarBajaEmpresaCliente(this);
        SwingPanel.getInstance().addWindow(p);
        p.setVisible(true);
}//GEN-LAST:event_btnDarBajaEmpresaClienteActionPerformed

    private void txtBuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtBuscarMouseClicked

        if(txtBuscar.getText().equals("Buscar...")) {
            txtBuscar.setText("");
        }
    }//GEN-LAST:event_txtBuscarMouseClicked

    private void txtBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyPressed
        // TODO add your handling code here:
        //elQueOrdena.setRowFilter(RowFilter.regexFilter(txtBuscar.getText()));
    }//GEN-LAST:event_txtBuscarKeyPressed

    private void rbAltasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbAltasActionPerformed
        elQueOrdena.setRowFilter(RowFilter.regexFilter("creada", 4));
    }//GEN-LAST:event_rbAltasActionPerformed

    private void rbBajasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbBajasActionPerformed
        elQueOrdena.setRowFilter(RowFilter.regexFilter("borrada", 4));
    }//GEN-LAST:event_rbBajasActionPerformed

    private void rbTodasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbTodasActionPerformed
        elQueOrdena.setRowFilter(null);
    }//GEN-LAST:event_rbTodasActionPerformed

    private void txtBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBuscarActionPerformed
        txtBuscar.setText("");
    }//GEN-LAST:event_txtBuscarActionPerformed

    private void txtBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyReleased
        // TODO add your handling code here:
        FiltrarTabla();
    }//GEN-LAST:event_txtBuscarKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConsultarEmpresaCliente;
    private javax.swing.JButton btnDarBajaEmpresaCliente;
    private javax.swing.JButton btnModificarEmpresaCliente;
    private javax.swing.JButton btnRegistrarEmpresaCliente;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbAltas;
    private javax.swing.JRadioButton rbBajas;
    private javax.swing.JRadioButton rbTodas;
    private javax.swing.JTable tablaEmpresaCliente;
    private javax.swing.JTextField txtBuscar;
    // End of variables declaration//GEN-END:variables

    public String getTituloAyuda() {
        return "Dar de Baja una Empresa Cliente";
    }

    public String getResumenAyuda() {
        return "Por medio de esta ventana usted puede dar de baja una empresa cliente de la empresa. Los datos permanecerán pero no será considerada para futuras transacciones.";
    }

    public int getIdAyuda() {
        return 0;
    }

    public void actualizar(int flag, boolean exito) {
        this.llenarTabla();
    }

}
